/**
 * 添加通知公告页面JS
 * @author jin.congshan
 * 
 */

var noticeEditor = null;
NoticeEdit = function(){
	K = KindEditor;
	return {
		init : function(){
			NoticeEdit.initKindEditor();
		},
		initKindEditor : function(){
			noticeEditor = K.create('#editor_id', {
				allowImageUpload	: true,
				uploadJson			: basePath+'/pc/notice/uploadImage.htm',
				filePostName		: 'imgFile',
				basePath : basePath+'/resources/js/common/kindeditor/'
			});
		},
		submitForm : function(state){
			$("#id_state").val(state);
			if(!$('#noticeForm').form('validate')){
				return;
			}
			noticeEditor.sync();
			if(noticeEditor.isEmpty()){
				Ict.warn("请输入通知内容!");
				return;
			}
			var title = $("#id_title").val();
			var newsId = $("#id_newsId").val();
			$.ajax({
				url		:	basePath+"/pc/notice/checkNoticeTitle.htm",
				type	:	'POST',
				data	:	{"title" : title, "newsId" : newsId, "newsType" : '0'},
				dataType:	'JSON',
				async	:	false,
				success :	function(data) {
					if(data && 1 == data.error_code){
						Ict.confirm("已存在该标题通知公告,是否继续?", function(yes){
							if(yes){
								NoticeEdit.doSubmit();
							}
						});
					} else if(0 == data.error_code) {
						NoticeEdit.doSubmit();
					} else {
						Ict.warn("未知错误");
					}
				}
			});
		},
		// 提交表单
		doSubmit : function() {
			$('#noticeForm').form('submit',{
				url:"/pc/notice/saveNoticeInfo.htm",
				onSubmit: function(){
					return $('#noticeForm').form('validate');
				},
				success:function(data){
					var json = $.parseJSON(data);
					if(data && 0 == json.error_code){
						Ict.info("保存成功");
						Ict.closeWin();
					} else {
						Ict.info("保存失败");
					}
				}
			});
		},
		closeWin : function() {
			parent.$('#commonWindow').window('close');
		}
	};
}();

$(function(){
	NoticeEdit.init();
});